package com.zsw.alancomicserver.repository;

import com.zsw.alancomicserver.repository.po.BookItemPO;
import com.zsw.alancomicserver.repository.po.BookPO;
import com.zsw.alancomicserver.repository.rowMapper.BookItemRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class BookItemRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public int insert(BookItemPO bookItemPO) {
        String sql = "insert into book_item(book_item_id, book_item_imgs, create_time, update_time, book_item_title, book_id) values(?,?,?,?,?,?)";
        return jdbcTemplate.update(sql, bookItemPO.getBookItemId(), bookItemPO.getBookItemImgs(), bookItemPO.getCreateTime(), bookItemPO.getUpdateTime(),
                bookItemPO.getBookItemTitle(), bookItemPO.getBookId());
    }

    public BookItemPO queryByBookItemId(Long bookItemId) {
        String sql = "select * from book_item where book_item_id=?";
        return jdbcTemplate.queryForObject(sql, new BookItemRowMapper(), bookItemId);
    }

    public List<BookItemPO> queryLatest20() {
        String sql = "select * from book_item order by id desc limit 20";
        return jdbcTemplate.query(sql, new BookItemRowMapper());
    }

    public List<BookItemPO> queryByBookId(Long bookId) {
        String sql = "select * from book_item where book_id=?";
        return jdbcTemplate.query(sql, new BookItemRowMapper(), bookId);
    }
}
